package com.xwnet.org.service.impl;


import com.xwnet.basic.service.impl.BasicServiceImpl;
import com.xwnet.org.domain.Department;
import com.xwnet.org.mapper.DepartmentMapper;
import com.xwnet.org.service.IDepartmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.Serializable;
import java.util.Date;
import java.util.List;
@Service
public class DepartmentServiceImpl extends BasicServiceImpl<Department> implements IDepartmentService {
    @Autowired
    private DepartmentMapper departmentMapper;
    @Override
    public List<Department> getDeptTree() {
        return departmentMapper.loadDeptTree();
    }

    @Override
    public void add(Department department) {
        Long deptId = department.getId();
        //生成path
        String path = "";
        if (department.getParent()==null || department.getParent().getId()==null){
            //1 判断是否是顶级部门，如果是，自己生成  /id
            path = "/"+deptId;
        }else{
            //2 /父部门ID/子部门id
            Long pid = department.getParent().getId();
            String pPath = super.getById(pid).getPath();
            path = pPath+"/"+deptId;
        }
        //设置path
        department.setPath(path);
        //设置添加事件
        department.setCreate_time(new Date());
        super.add(department);
    }

    @Override
    public void update(Department department) {
        Long deptId = department.getId();
        //生成path
        String path = "";
        if (department.getParent()==null || department.getParent().getId()==null){
            //1 判断是否是顶级部门，如果是，自己生成  /id
            path = "/"+deptId;
        }else{
            //2 否则parentPath+/id
            Long pid = department.getParent().getId();
            String pPath = super.getById(pid).getPath();
            path = pPath+"/"+deptId;
        }
        //设置path
        department.setPath(path);
        //设置更改时间
        department.setUpdate_time(new Date());
        super.update(department);
    }
}
